package net.xinqushi.utils;

import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class MyDateUtils {
    public static String yyyyMMdd="yyyyMMdd";
    public static String yyyyMMddHHmmss="yyyyMMddHHmmss";
    public static String yyyyMMddHHmmssSSS="yyyyMMddHHmmssSSS";
    public static String yyyyMMddTHHmmss="yyyy-MM-dd HH:mm:ss";
    public static String HHmmss="HHmmss";

    
    public static Date strToDate(String strDate) {
    	if (StringUtils.isBlank(strDate)) {
    		return null;
    	}
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    public static Date strToDate(String strDate,String format) {
        if (StringUtils.isBlank(strDate)) {
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    public static String getString2Date(Date time) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(time);
        return dateString;
    }

    public static String getString2Date(Date time,String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateString = formatter.format(time);
        return dateString;
    }

	/**  
     * 计算两个日期之间相差的天数  
     * @param smdate 较小的时间 
     * @param bdate  较大的时间 
     * @return 相差天数 
     * @throws ParseException  
     */    
    public static int daysBetween(Date smdate,Date bdate)  
    {    
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
        try {
			smdate=sdf.parse(sdf.format(smdate));
			bdate=sdf.parse(sdf.format(bdate));
		} catch (ParseException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}  
        Calendar cal = Calendar.getInstance();    
        cal.setTime(smdate);    
        long time1 = cal.getTimeInMillis();                 
        cal.setTime(bdate);    
        long time2 = cal.getTimeInMillis();         
        long between_days=(time2-time1)/(1000*3600*24);  
        return Integer.parseInt(String.valueOf(between_days));           
    }

    /**
     * 得到差几天的时间 day:正负
     *
     * @param d
     * @param day
     * @return
     */
    public static Date getDateBeforeOrAfter(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.add(Calendar.DATE, day);
        return now.getTime();
    }

    /**
     * 获取入账日期
     * @param d
     * @param day
     * @return
     */
    public static Date getMergeDate(Date d, int day) {
        //按照每月28天计算
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        int date = now.get(Calendar.DATE);
        if(date-day>0){
            int i = date-28>0?date-28:0;
            now.set(Calendar.DATE,date-day-i);
        }else{
            now.add(Calendar.MONTH, -1);
            now.set(Calendar.DATE,28+date-day);
        }
        return now.getTime();
    }

    /**
     * 得到差几个月的时间 month:正负
     * @param d
     * @param month
     * @return
     */
    public static Date getDateAfterMonth(Date d, int month) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.add(Calendar.MONTH, month);
        return now.getTime();
    }

    /**日期加N天数*/
    public static Date addDays(Date date,int day){
    	Calendar instance = Calendar.getInstance();
    	instance.setTime(date);
    	instance.add(Calendar.DAY_OF_MONTH, day);
    	return instance.getTime();
    }
    
    public static int hoursBetween(Date smdate,Date bdate)  
    {    
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        try {
			smdate=sdf.parse(sdf.format(smdate));
			bdate=sdf.parse(sdf.format(bdate));
		} catch (ParseException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}  
        Calendar cal = Calendar.getInstance();    
        cal.setTime(smdate);    
        long time1 = cal.getTimeInMillis();                 
        cal.setTime(bdate);    
        long time2 = cal.getTimeInMillis();         
        long between_hours=(time2-time1)/(1000*3600);  
        return Integer.parseInt(String.valueOf(between_hours));           
    }

    public static int minutesBetween(Date smdate,Date bdate)
    {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            smdate=sdf.parse(sdf.format(smdate));
            bdate=sdf.parse(sdf.format(bdate));
        } catch (ParseException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_hours=(time2-time1)/(1000*60);
        return Integer.parseInt(String.valueOf(between_hours));
    }

    /**
	*字符串的日期格式的计算 
	*/  
    public static int daysBetween(String smdate,String bdate) throws ParseException{  
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");  
        Calendar cal = Calendar.getInstance();    
        cal.setTime(sdf.parse(smdate));    
        long time1 = cal.getTimeInMillis();                 
        cal.setTime(sdf.parse(bdate));    
        long time2 = cal.getTimeInMillis();         
        long between_days=(time2-time1)/(1000*3600*24);  
            
       return Integer.parseInt(String.valueOf(between_days));     
    }  
    
    public static String dateFormat(Date date,boolean displayMilliseconds){
		if(displayMilliseconds){
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return df.format(date);
		}else{
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			return df.format(date);
		}
	}
    public static String dateForyyyyMMdd(Date date){
    	DateFormat df = new SimpleDateFormat(yyyyMMdd);
    	return df.format(date);
    }
    
    public static String dataForHHmmss(Date date) {
    	Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY)+":"+calendar.get(Calendar.MINUTE)+":"+ calendar.get(Calendar.SECOND);
    }
    public static Date dateTOFormat(Date date,boolean displayMilliseconds){
    	if(displayMilliseconds){
    		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    		return strToDate(df.format(date), "yyyy-MM-dd HH:mm:ss");
    	}else{
    		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    		return strToDate(df.format(date), "yyyy-MM-dd");
    	}
    }

	public static String strToDateFormat(String date) {
		DateFormat df1 = new SimpleDateFormat("yyyyMMdd");
		DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
		String str = "";
		try {
			str = df2.format(df1.parse(date));
		} catch (ParseException e) {

		}
		return str;
	}
	
	/**
	 * 是否是工作时间
	 * 
	 * @param date
	 * @return
	 * @author FuMingming
	 * @date 2018年3月13日
	 */
	public static Boolean isWorkingTime(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("HH");
		Integer hour = Integer.valueOf(sdf.format(date));
		System.out.println(hour);
		return hour >= 9 && hour < 18;
	}

    public static String getStringDateFromTimestamp(Timestamp ts) {
        String tsStr = "";
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            tsStr = sdf.format(ts);
            return tsStr;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * date日期转换为‘yyyy/MM/dd’格式
     * @param date
     * @return
     */
    public static String getDateStr(Date date){
        DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
        String str = df.format(date);
        return str;
    }

    /**
     * @author qdd
     * @discription: xxxx-xx-xxTxx:xx:xx.xxx
     * @date 2019/7/24 14:50
     */
    public static String dateFormatV2(Date date){
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        String format = df.format(date);
        String[] s = format.split(" ");
        return s[0]+"T"+s[1]+"."+s[2];
    }

    /** 
     * @param args 
     * @throws ParseException  
     */  
    public static void main(String[] args) throws ParseException {  
       /* // TODO Auto-generated method stub
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date d1=sdf.parse("2017-02-14 23:59:59");  
        Date d2=sdf.parse("2017-02-15 10:18:17");
//        int x=daysBetween(d1, d2);
//        System.out.println(x);

        Calendar calendar = Calendar.getInstance();
		calendar.setTime(d2);
		int nowHour = calendar.get(Calendar.HOUR);
		System.out.println(nowHour);
        SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd");
        Date d3=sdf2.parse("2018-03-03");
        Date d4=getMergeDate(d3,18);
        System.out.println(sdf2.format(d4));*/
        System.out.println(dateFormatV2(new Date()));
    }

    public static String dateFormat(Date date , String format){
        DateFormat df = new SimpleDateFormat(format);//"yyyyMMdd HH:mm:ss"
        return df.format(date);
    }

    /**
     * @author qdd
     * @discription:  String转成日期格式yyyy-MM-dd(两参数格式要求一致)
     * @date 2019/7/9 16:17
     */
    public static Date dateStrToDate(String str ,String format) {
        try {
            Date date = new SimpleDateFormat(format).parse(str);
            return strToDate(new SimpleDateFormat("yyyy-MM-dd").format(date), "yyyy-MM-dd");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
	
	
